package com.dreamana.ui
{
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;

	public final class SkinGraphics
	{
		public function SkinGraphics()
		{
		}
		
		/**
		 * Fill bitmap texture 
		 * @param bitmapData
		 * @param w
		 * @param h
		 * @param smooth
		 */		
		public static function fillRect(graphics:Graphics, bitmapData:BitmapData, w:Number, h:Number, matrix:Matrix=null, smooth:Boolean=false):void
		{
			graphics.beginBitmapFill(bitmapData, matrix, true, smooth);
			graphics.drawRect(0, 0, w, h);
			graphics.endFill();
		}
		
		/**
		 * Fill bitmap texture with Scale9Grid style
		 * inspired by ScaleBitmap(http://www.bytearray.org/?p=1206, by Didier Brun)
		 * 
		 * @param bitmapData
		 * @param scale9Grid
		 * @param w
		 * @param h
		 * @param smooth
		 */		
		public static function fill9Grid(graphics:Graphics, bitmapData:BitmapData, w:Number, h:Number, scale9Grid:Rectangle, smooth:Boolean=false):void
		{
			var widths:Array = [scale9Grid.left + 1, scale9Grid.width - 2, bitmapData.width - scale9Grid.right + 1];
			var heights:Array = [scale9Grid.top + 1, scale9Grid.height - 2, bitmapData.height - scale9Grid.bottom + 1];
			
			//draw width of middle part
			var mw:Number = w - widths[0] - widths[2];
			var mh:Number = h - heights[0] - heights[2];
			
			var left:Number = 0;
			var top:Number = 0;
			
			var j:int, i:int;
			var ox:Number=0, oy:Number=0;
			var dx:Number=0, dy:Number=0;
			var ow:Number, oh:Number;
			var dw:Number, dh:Number;
			var mtx:Matrix = new Matrix();
			
			// let's draw
			for(j=0; j < 3; ++j)
			{
				// original width
				ow = widths[j];
				// draw width						
				dw = (j==1)? mw : ow;
				
				// original & draw offset
				dy = oy = 0;
				mtx.a = dw / ow;
				
				for(i=0; i < 3; ++i)
				{
					// original height
					oh = heights[i];
					// draw height
					dh = (i==1)? mh : oh;
					
					if (dw > 0 && dh > 0)
					{
						// some matrix computation
						mtx.d = dh / oh;
						mtx.tx = -ox * mtx.a + dx;
						mtx.ty = -oy * mtx.d + dy;
						//mtx.translate(left, top);
						
						// draw the cell
						graphics.beginBitmapFill(bitmapData, mtx, false, smooth);
						graphics.drawRect(dx + left, dy + top, dw, dh);
						graphics.endFill();
					}
					
					// offset incrementation
					oy += oh;
					dy += dh;
				}
				
				// offset incrementation
				ox += ow;
				dx += dw;
			}
			
			//question: endFill here better?
		}
		
		/**
		 * Draw color only
		 * @param graphics
		 * @param w
		 * @param h
		 * @param color
		 */			
		public static function drawBlank(graphics:Graphics, w:Number, h:Number, color:uint=0xffffff):void
		{
			graphics.lineStyle();
			graphics.beginFill(color, 1.0);
			graphics.drawRect(0, 0, w, h);
			graphics.endFill();
		}
		
		/**
		 * Draw wireframe only
		 * @param graphics
		 * @param w
		 * @param h
		 */		
		public static function drawWireFrame(graphics:Graphics, w:Number, h:Number):void
		{
			if(debug) {
				graphics.lineStyle();
				graphics.beginFill(0xffffff);
				graphics.drawRect(0, 0, w, h);
				graphics.endFill();
				
				graphics.lineStyle(0, 0x0);
				graphics.drawRect(0, 0, w-1, h-1);
			}
			else {
				graphics.drawRect(0, 0, w, h);
			}
		}
		
		public static var debug:Boolean = true;
	}
}